//
//  SetUpViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/3/26.
//

#import "SetUpViewController.h"
#import "ViewController.h"
#import "Info1ViewController.h"
@interface SetUpViewController ()<UITextFieldDelegate,TZImagePickerControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (nonatomic , strong) UITextField * textField;
@property (nonatomic , strong) SQCustomButton * avatarImage;
@property (nonatomic , assign) BOOL  isEdit;
@end

@implementation SetUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = UIColor.blackColor;
    self.avatarImage.image = SQGetImage(@"上传图片");
    [HttpTool post:Url6 paramDic:[[NSMutableDictionary alloc]init] successBlock:^(BaseModel * _Nonnull httpModel) {
        [MeModel SQ_setuserModel:httpModel];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}

- (void)SQ_UIViewForSelfView{
    for (int x = 0; x<3; x++) {
        UIView * bglineview = [[UIView alloc]init];
        if (x==0) {
            bglineview.backgroundColor = SQColor(@"#BF33FF");
        } else {
            bglineview.backgroundColor = SQColor(@"#414141");
        }
        [self.view addSubview:bglineview];
        [bglineview mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(SCREEN_WIDETH/3.f-4);
                    make.height.mas_equalTo(2);
                    make.left.equalTo(self.view).offset(SCREEN_WIDETH/3.f*x);
            make.bottom.equalTo(self.zx_navBar.mas_bottom);
        }];
    }
    
    UILabel * num = [[UILabel alloc]init];
    num.text = SQNSLocalString(@"1/3");
    num.font = FONT_MEDIUM(13);
    num.textColor = SQColor(@"#6B6B6B");
    [self.view addSubview:num];
    [num mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.view.mas_right).offset(-24);
            make.top.equalTo(self.zx_navBar.mas_bottom).offset(8);
    }];
    
    UIImageView * topImage = [[UIImageView alloc]init];
    topImage.image = SQGetImage(@"椭圆形");
    topImage.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:topImage];
    [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(12);
            make.top.equalTo(self.zx_navBar.mas_bottom).offset(10);
            make.size.mas_equalTo(68);
    }];
    UILabel * label1 = [[UILabel alloc]init];
    label1.text = SQNSLocalString(@"你好~");
    label1.textColor = UIColor.whiteColor;
    label1.font = FONT_NIHAO(32);
    [self.view addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(35);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(25);
        
    }];
    
    UILabel * num2 = [[UILabel alloc]init];
    num2.text = SQNSLocalString(@"完善一下资料吧");
    num2.font = FONT_SEMIBOLD(18);
    num2.textColor = UIColor.whiteColor;
    [self.view addSubview:num2];
    [num2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(35);
        make.top.equalTo(label1.mas_bottom).offset(8);
    }];
    UILabel * num3 = [[UILabel alloc]init];
    num3.text = SQNSLocalString(@"头像");
    num3.font = FONT_MEDIUM(17);
    num3.textColor = UIColor.whiteColor;
    [self.view addSubview:num3];
    [num3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(35);
        make.top.equalTo(num2.mas_bottom).offset(65);
    }];
    
    [self.view addSubview:self.avatarImage];
    [self.avatarImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(96);
        make.left.equalTo(self.view).offset(50);
        make.top.equalTo(num3.mas_bottom).offset(16);
    }];
 
    UILabel * label2 = [[UILabel alloc]init];
    label2.text = SQNSLocalString(@"昵称");
    label2.textColor =  UIColor.whiteColor;;
    label2.font = FONT_MEDIUM(17);
    [self.view addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(35);
        make.top.equalTo(num3.mas_bottom).offset(167);
    }];
   
    [self.view addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(44);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-70);
        make.top.equalTo(label2.mas_bottom).offset(16);
    }];
    
    HeaderView * xiayibu = [[HeaderView alloc]init];
    xiayibu.image = @"下一步";
    [xiayibu.btn addTarget:self action:@selector(SQ_tijiaoButton) forControlEvents:UIControlEventTouchUpInside];
    xiayibu.font = FONT_MEDIUM(18);
    [self.view addSubview:xiayibu];
    [xiayibu mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(72);
        make.bottom.equalTo(self.view.mas_bottom).offset(-88-FootHeight);
    }];
    WS(weakSelf);
    [self.avatarImage setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_headerButtonClick];
    }];
}
 
-(void)SQ_tijiaoButton{
    if (!self.isEdit) {
        [YJProgressHUD showMsgWithoutView:@"请设置头像!"];
        return;
    }
    [self.view endEditing:YES];
    if (![self.textField.text isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:@"请输入昵称!"];
        return;
    }
    if (self.textField.text.length > 8||self.textField.text.length<2) {
        [YJProgressHUD showMsgWithoutView:@"昵称长度为2至8个字符"];
        return;
    }
    [self SQ_caijian];
}

-(void)SQ_headerButtonClick{
    WS(weakSelf);
    [self.view endEditing:YES];
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"设置头像") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_leftButtonClick];
    }];
    UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"相册") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_rightButtonClick];
    }];
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co1];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)SQ_leftButtonClick{
    WS(weakSelf);
    _imagePickerVc = [[UIImagePickerController alloc] init];
    _imagePickerVc.delegate = self;
    _imagePickerVc.allowsEditing = YES;
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: sourceType]) {
        weakSelf.imagePickerVc.sourceType = sourceType;
        [self presentViewController:weakSelf.imagePickerVc animated:YES completion:nil];
    } else {
        NSLog(@"模拟器中无法打开照相机,请在真机中使用");
    }
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    WS(weakSelf);
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *photo = [info objectForKey:UIImagePickerControllerEditedImage];
        if (photo == nil)
            {
                photo = [info objectForKey:UIImagePickerControllerOriginalImage];
            }
        weakSelf.avatarImage.image = photo;
        weakSelf.isEdit = YES;
    }else
    {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"相册数据异常")];
    }
}


-(void)SQ_rightButtonClick{
    WS(weakSelf);
    TZImagePickerController * vc = [[TZImagePickerController alloc]init];
    vc.maxImagesCount = 1;
    vc.allowTakeVideo = NO;
    vc.allowPickingVideo = NO;
    [self presentViewController:vc animated:YES completion:nil];
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        weakSelf.avatarImage.image = photos.firstObject;
        weakSelf.isEdit = YES;
    }];
}
//上传相册
-(void)SQ_caijian{
   
    WS(weakSelf);
    //上传头像
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [HttpTool SQ_Image:self.avatarImage.image successBlock:^(BaseModel * _Nonnull httpModel) {
        [weakSelf SQ_setUpName:httpModel.data];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_setUpName:(NSString*)header{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:header forKey:@"portrait"];
    [dic setObject:self.textField.text forKey:@"name"];
    [HttpTool post:Url5 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        Info1ViewController * vc = [[Info1ViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
 

- (UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc]init];
        _textField.backgroundColor = SQColor(@"#414141");
        _textField.textColor = UIColor.whiteColor;
        _textField.font = FONT_MEDIUM(17);
        _textField.delegate = self;
        _textField.placeholder = SQNSLocalString(@"想好一个好听的昵称吧~");
        [_textField setValue:SQColor(@"#6B6B6B") forKeyPath:@"placeholderLabel.textColor"];
        _textField.returnKeyType = UIReturnKeyDone;
        _textField.layer.masksToBounds = YES;
        _textField.layer.cornerRadius = 8;
        _textField.textAlignment = NSTextAlignmentCenter;
        
    }
    return _textField;
}
- (SQCustomButton *)avatarImage{
    if (!_avatarImage) {
        _avatarImage = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(96, 96) midmargin:0];
        _avatarImage.layer.cornerRadius = 48;
        _avatarImage.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImage.imageView.clipsToBounds = YES;
        _avatarImage.backgroundColor = UIColor.clearColor;
        _avatarImage.clipsToBounds  = YES;
    }
    return _avatarImage;
}
@end
